package com.css.fxfzaqbz.modules.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.common.utils.ZipDownloadUtil;
import com.css.fxfzaqbz.modules.base.entity.HouseData;
import com.css.fxfzaqbz.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

@RestController
@RequestMapping("/fxfzaqbz/baseData")
@Slf4j
public class BaseDataController {

    @Resource
    RestTemplateUtil restTemplateUtil;

    //获取配置文件的上传文件路径
    @Value("${file.upload.path}")
    private String uploadPath;

    @GetMapping("/getDataTypeLevel")
    public String getDataTypeLevel(){
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("restTemplateUrl") + "/test/dictItems/getDataTypeLevel2AndDataTypeLevel3Tree";
        Map<String, Object> paramMap = new HashMap<>();
        ResponseEntity<String> cchResponseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
      //  List<Map<String, Object>> maps = JSONObject.parseArray(cchResponseEntity.getBody(), Map<String, Object>.class);
        String body = cchResponseEntity.getBody();
        return body;
    }

    //下载模板
    @PostMapping(value = "/downloadShpZip")
    public RestResponse downloadShpZip(String fileName, HttpServletResponse response) {
        log.info("入参: 空间数据模板名全称", fileName);
        String newUploadPath = uploadPath + fileName + "/";
        try {
            //获取指定路径下的所有文件名
            File file = new File(newUploadPath);
            File[] files = file.listFiles();
            if (files == null) {
                return RestResponse.fail("下载失败，该目录下不存在文件");
            }
            String tempFileName = fileName;
            String _temp = "";
            List<File> _files = new ArrayList<>();
            for (int i = 0; i < files.length; i++) {
                _temp = files[i].getName();
                if (_temp.indexOf(tempFileName) != -1) _files.add(files[i]);
            }
            List<File> zipFiles = _files;

            //List<File> zipFiles = new ArrayList<>(Arrays.asList(files));
            StringBuffer sb = new StringBuffer();
            if (!CollectionUtils.isEmpty(zipFiles)) {
                String zipName = fileName + ".zip";
                String msg = ZipDownloadUtil.zipDownload(response, newUploadPath, zipName, zipFiles);
                sb.append(msg);
            } else {
            /*sb.append("没有可下载文件");
            return RestResponse.succeed(sb);*/
                String zipName = fileName + ".zip";
                String msg = ZipDownloadUtil.zipDownload(response, newUploadPath, zipName, new ArrayList<File>());
                sb.append(msg);
            }
            log.info(fileName + ": 空间数据模板下载完成");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(fileName + ":" + e);
        }

        return null;
    }
}
